//
//  AXHomeXxtydTjVC.m
//  AnXin100
//
//  Created by 天质弘耕 on 2018/10/26.
//  Copyright © 2018年 tzhg. All rights reserved.
//

#import "AXHomeXxtydTjVC.h"
#import "UIView+SDAutoLayout.h"
#import "ZXCategories.h"
#import "Header.h"
#import "UITableView+SDAutoTableViewCellHeight.h"
#import "AXHomeXxtydTjCell.h"
#import "AXHomeXxtydTjModel.h"
#import "StoreViewController.h"


@interface AXHomeXxtydTjVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray<AXHomeXxtydTjModel *>  *modelsArray;
@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic ,strong) UIView *headView;
/** 假图片数据 */
@property (nonatomic, strong) NSArray<NSString *> *imageNames;
@property (nonatomic, strong) NSArray<NSString *> *titleNames;
@property (nonatomic, strong) NSArray<NSString *> *priceNames;
/** 搜索按钮 */
@property (nonatomic ,strong) UIButton *searchButton;
@property (nonatomic , assign) NSInteger requstIndex;

@end

@implementation AXHomeXxtydTjVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatView];
    [self setlayout];
    [self setupNavigationItems];

    // 下拉刷新
    self.tableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self requestList];
        [self.tableView.mj_header endRefreshing];
        
    }];
    
    
    // 上拉刷新
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        [self requestList];
        [self.tableView.mj_footer endRefreshing];
    }];
    
}

- (void)creatView {
    
    self.tableView = [[UITableView alloc]init];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[AXHomeXxtydTjCell class] forCellReuseIdentifier:[AXHomeXxtydTjCell reuseID]];
    [self.view addSubview:self.tableView];
    
    
    
}


- (void)setlayout {
    
    
    self.tableView.sd_layout
    .leftSpaceToView(self.view, 0)
    .topSpaceToView(self.view, 0)
    .widthIs(Wi)
    .heightIs(MyTableViewHeight);
    
    
}

- (void)setupNavigationItems {
    
    /** 导航条左边的按钮 */
    //返回按钮
    UIButton *kfBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [kfBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    kfBtn.frame = CGRectMake(0, 0, 30, 30);
    [kfBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:kfBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    /** 搜索 */
    self.searchButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-100, 30)];
    self.searchButton.backgroundColor = [UIColor whiteColor];
    self.searchButton.titleLabel.font = FONT_TWELVE;
    [self.searchButton addTarget:self action:@selector(search:) forControlEvents:UIControlEventTouchUpInside];
    [self.searchButton setTitleColor:[UIColor colorWithHex:0x29b566] forState:UIControlStateNormal];
    [self.searchButton setTitle:@"应季水果" forState:UIControlStateNormal];
    self.searchButton.cornerRadius = 15;
    [self.searchButton setImage:[UIImage imageNamed:@"ss"] forState:UIControlStateNormal];
    self.navigationItem.titleView = self.searchButton;
    
    
}

#pragma mark - Action

- (void)backAction:(UIBarButtonItem *)item {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)search:(UIButton *)btn {
    
}

#pragma mark - requestNetwork

- (void)requestList{
    
     MBProgressHUD *hud = [WHRNetwork showHudAtView:self.view message:@"正在加载..." isTextOnly:NO];
    ZX_WEAK_SELF
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"actitvityListType"] = @"UNDERWAY";
    params[@"storeCategory"] = @1;
    params[@"origLng"] = @"25.072285";
    params[@"origLat"] = @"102.7298";
    
    [NetWorkHelp yl_postDataWith:params url:URL_SPLIT(URL_Store, URL_Store_selectStoreListInfo) success:^(id respose) {
        
        
        if ([respose[@"header"][@"statusCode"] integerValue] == 200) {
            
            weakSelf.modelsArray = [AXHomeXxtydTjModel mj_objectArrayWithKeyValuesArray:respose[@"body"][@"storeListInfoVOs"]];
            [self.tableView reloadData];
            
        } else if ([respose[@"header"][@"statusCode"] integerValue] != 200) {
            
            [hud hideAnimated:YES];
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
        }
        
        
    } failure:^(NSError *error) {
        
    }];
    
}



#pragma mark - tableViewDelegate & tableViewDatasorce



//几个分区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.modelsArray.count;
}

/** cell的高度 */

/** cell 自适应第二步 */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return [tableView cellHeightForIndexPath:indexPath model:self.modelsArray[indexPath.row] keyPath:@"model" cellClass:[AXHomeXxtydTjCell class] contentViewWidth:SCREEN_WIDTH];
    
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    AXHomeXxtydTjCell *cell = [tableView dequeueReusableCellWithIdentifier:[AXHomeXxtydTjCell reuseID] forIndexPath:indexPath];
    cell.model = self.modelsArray[indexPath.row];
  
    
    return cell;
    
   
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    StoreViewController *vc = [StoreViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}


@end
